import type { ProColumns } from '@/components/Pro/';
import { ProTable } from '@/components/Pro/';
import {
  eventDataControllerGetEventTypeLevel as getEventTypeLevel,
  eventDataControllerGetPageEventList as getList,
} from '@/services/ess/eventData';
import { DATABASE_TYPE_ENUM, fetchDictInfo } from '@/utils';
import type { DatePickerProps } from 'antd';
import moment from 'moment';
import React, { useEffect, useState } from 'react';
import { useIntl, useModel } from 'umi';

const startTime = ' 00:00:00';
const endTime = ' 23:59:59';

const TableList: React.FC = () => {
  const { stationId } = useModel('@@initialState')?.initialState || {};
  const [eventTypes, setEventType] = useState([]);
  const intl = useIntl();

  useEffect(() => {
    getEventTypeLevel().then((res) => {
      if (res?.success) {
        setEventType(res?.data ?? []);
      }
    });
  }, []);

  const disabledDate: DatePickerProps['disabledDate'] = (current, { from }) => {
    const disabled = current && current >= moment().endOf('day');

    if (from) {
      return disabled || Math.abs(current.diff(from, 'month')) >= 3;
    }

    return disabled;
  };
  const columns: ProColumns<API.EventMsg>[] = [
    {
      title: intl.formatMessage({ id: 'alarmManagement.generalProcedure.SN' }),
      dataIndex: 'pageIndex',
      hideInSearch: true,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.deviceType' }),
      dataIndex: 'deviceType',
      request: () => fetchDictInfo(DATABASE_TYPE_ENUM.EVENT_DEVICE_TYPE),
      order: 100,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventGrade' }),
      dataIndex: 'level',
      request: () => fetchDictInfo(DATABASE_TYPE_ENUM.EVENT_GRADE),
      valueType: 'select',
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventCategory' }),
      dataIndex: 'seniorType',
      fieldProps: {
        options: eventTypes,
        fieldNames: {
          label: 'eventTypeName',
          value: 'eventType',
          children: 'subTypes2',
        },
      },
      valueType: 'select',
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventSubcategory' }),
      dataIndex: 'juniorType',
      fieldProps: {
        options: eventTypes,
        fieldNames: {
          label: 'eventTypeName',
          value: 'eventType',
          children: 'subTypes',
        },
      },
      //request: () => fetchDictInfo(DATABASE_TYPE_ENUM.EVENT_SMALL_CATEGORY),
      valueType: 'select',
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.Eventstatus' }),
      dataIndex: 'eventState',
      valueEnum: {
        1: {
          text: intl.formatMessage({ id: 'alarmManagement.alarmList.Occurring' }),
          status: '1',
        },
        0: {
          text: intl.formatMessage({ id: 'alarmManagement.alarmList.Disappeared' }),
          status: '0',
        },
      },
      order: 2,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventLocation' }),
      dataIndex: 'location',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventName' }),
      dataIndex: 'eventName',
      width: 150,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventData' }),
      dataIndex: 'eventDesc',
      width: 220,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.eventCoding' }),
      dataIndex: 'code',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.collectionTime' }),
      dataIndex: 'collectTime',
      valueType: 'dateRange',
      order: 150,
      width: 150,
      initialValue: [moment(new Date()).startOf('day'), moment()],
      fieldProps: {
        disabledDate: disabledDate,
        allowClear: false,
      },
      search: {
        transform: (value) => {
          return {
            collectStartTime: value[0] + startTime,
            collectEndTime: value[1] + endTime,
          };
        },
      },
      render: (_, record) => record.collectTime,
    },
    {
      title: intl.formatMessage({ id: 'alarmManagement.alarmList.ReportingTime' }),
      dataIndex: 'writeTime',
      valueType: 'dateRange',
      width: 150,
      fieldProps: {
        disabledDate: disabledDate,
      },
      search: {
        transform: (value) => {
          return {
            reportStartTime: value[0] + startTime,
            reportEndTime: value[1] + endTime,
          };
        },
      },
      render: (_, record) => record.reportTime,
    },
  ];

  return (
    <div>
      <ProTable<API.EventMsg, API.PageParams>
        headerTitle={intl.formatMessage({ id: 'alarmManagement.alarmList.AlarmList' })}
        rowKey="id"
        request={(params) => {
          return getList(params);
        }}
        params={{
          stationId,
        }}
        columns={columns}
      />
    </div>
  );
};

export default TableList;
